package cn.scw.common.utils;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 字节工具类
 */
public class ByteUtil {

    public ByteUtil() {
    }

    public static int toInt(byte[] src) {
        switch(src.length) {
            case 1:
                return src[0] & 255;
            case 2:
                return src[1] & 255 | (src[0] & 255) << 8;
            case 3:
                return src[2] & 255 | (src[1] & 255) << 8 | (src[0] & 255) << 16;
            default:
                return src[3] & 255 | (src[2] & 255) << 8 | (src[1] & 255) << 16 | (src[0] & 255) << 24;
        }
    }

    public static int toInt(byte[] src, int startIndex, int endIndex) {
        if (startIndex >= 0 && endIndex >= 0 && startIndex <= src.length && endIndex <= src.length && endIndex >= startIndex) {
            if (endIndex - startIndex > 3) {
                endIndex = startIndex + 3;
            }

            byte[] array = new byte[endIndex - startIndex + 1];
            System.arraycopy(src, startIndex, array, 0, endIndex - startIndex + 1);
            return toInt(array);
        } else {
            return 0;
        }
    }

    public static byte[] intToByteArray(int a) {
        return new byte[]{(byte)(a >> 24 & 255), (byte)(a >> 16 & 255), (byte)(a >> 8 & 255), (byte)(a & 255)};
    }

    public static byte[] hexStringTobytes(String src) {
        byte[] arrB = src.getBytes();
        int iLen = arrB.length;
        byte[] arrOut = new byte[iLen / 2];

        for(int i = 0; i < iLen; i += 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte)Integer.parseInt(strTmp, 16);
        }

        return arrOut;
    }

    public static String bytesToBinaryString(byte[] src) {
        StringBuffer result = new StringBuffer();

        for(int i = 0; i < src.length; ++i) {
            result.append(addZeroForNum(Integer.toString(src[i] & 255, 2), 8));
        }

        return result.toString();
    }

    public static byte[] binaryStringToBytes(String src, Integer length) {
        src = StringUtils.trimAllWhitespace(src);
        List<Byte> listResult = new ArrayList();

        int resultLength;
        for(resultLength = 0; resultLength * 8 < src.length(); ++resultLength) {
            int endIndex = resultLength * 8 + 8 < src.length() ? resultLength * 8 + 8 : src.length();
            listResult.add(Long.valueOf(src.substring(resultLength * 8, endIndex), 2).byteValue());
        }

        resultLength = length == null ? listResult.size() : length;
        byte[] result = new byte[resultLength];

        for(int i = 0; i < result.length; ++i) {
            if (i >= result.length - listResult.size()) {
                result[i] = (Byte)listResult.get(i - (result.length - listResult.size()));
            } else {
                result[i] = 0;
            }
        }

        return result;
    }

    private static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();

        for(StringBuffer sb = null; strLen < strLength; strLen = str.length()) {
            sb = new StringBuffer();
            sb.append("0").append(str);
            str = sb.toString();
        }

        return str;
    }
}
